#include "GameClientThreadModel.h"
#include "GlobalClass.h"

GameClientThreadModel::GameClientThreadModel(QObject *parent)
    :QThread(parent)
{
    m_eGameStatus = Game_Status_GameOver;
    m_bInitOnceInThread = false;
    m_bInitOnce = false;
}

GameClientThreadModel::~GameClientThreadModel()
{
}

int GameClientThreadModel::InitOnce()
{
    if(m_bInitOnce)
    {
        return 1;
    }

    m_bInitOnce = true;
    emit s_InitOnce();
    qDebug() << "InitOnce over";
    return 0;
}

int GameClientThreadModel::InitOnceInThread()
{
    if(m_bInitOnceInThread)
    {
        return 1;
    }
    m_bInitOnceInThread = true;
    qDebug() << "InitOnceInThread over";
    return 0;
}

int GameClientThreadModel::GameInit(int sr)
{
    emit s_Debug(_T("[GameClientThreadModel]随机数种子:%1").arg(lmsSrand(sr)));
    m_eGameStatus = Game_Status_Continue;

    emit s_GameInit();
    return 0;
}


GameClientThreadModel::_GameStatus GameClientThreadModel::GameBegin()
{
    //Sleep(1000);
    emit s_GameBegin();

    return GameLoop();
}

int GameClientThreadModel::GameOver()
{
    emit s_Debug("游戏结束");
    emit s_GameOver(gameStatus());
    //qDebug()<<pWordLib->rightCount()<<pWordLib->errCount();
    return 0;
}

void GameClientThreadModel::run()
{
    /* 线程的相关代码 */

    InitOnceInThread();
    if(GameInit(m_nGameRandom) < 0)
    {
        emit s_Critical(_T("[GameClientThreadModel]游戏初始化失败!"));
        return;
    }
    GameBegin();
    GameOver();
}

